package cpoo.globalMotion;

public class PartialSum {
	private int sum = 0;
	private int cutFromLeft = 0;
	private int cutFromRight = 0;
	
	public PartialSum (int sum, int cutFromLeftTop, int cutFromRigthBottom){
		this.sum = sum;
		this.cutFromLeft = cutFromLeftTop;
		this.cutFromRight = cutFromRigthBottom;
	}
	
	public void moveRight (int sub, int add){
		moveRightOrDown (sub,add);
	}
	public void moveDown (int sub, int add){
		moveRightOrDown (sub,add);
	}
	
	public void moveRightOrDown (int sub, int add){
		if (add < 0 ){
			cutFromRight ++;
			add = 0;
		}
		if (sub < 0){
			cutFromLeft --;
			//cutFromLeft = cutFromLeft<0?0:cutFromLeft;
			sub = 0;
		}
		sum += add-sub;
	}
	
	public void moveLeft (int sub, int add){
		moveLeftOrUp (sub,add);
	}
	public void moveUp (int sub, int add){
		moveLeftOrUp (sub,add);
	}
	public void moveLeftOrUp (int sub, int add){
		if (add < 0 ){
			cutFromLeft ++;
			add = 0;
		}
		if (sub < 0){
			cutFromRight --;
			cutFromRight = cutFromRight<0?0:cutFromRight;
			sub = 0;
		}
		sum += add-sub;
	}
	
	public int compare(PartialSum other){
		if (sum == -1 || other.sum == -1){
			return 0;
		}
		if (other.cutFromLeft == cutFromLeft && other.cutFromRight == cutFromRight){
			return Math.abs(sum - other.sum);
		}
		return -1;
	}
	
	public int leftDisplacement(){
		return cutFromLeft;
	}
	
	public int rightDisplacement(){
		return cutFromRight;
	}
	
	public int getSum(){
		return sum;
	}
}
